import '../../../assets/js/libs/jquery.ztree.all.min.js'

export default {
  props:{
    isNeedAreaTree: [String, Boolean],
    isCheckbox: {
      type: Boolean,
      default: false
    },
    paramNameForAreaTree: {
      type: String,
      default: 'areaIds'
    },
    selectedAreaNode:{
      type: String,
      default: ''
    },
    areaTreeData:Array,
    channelTreeData:Array,
    reloadChannelTree:{
      type: Boolean,
      default: false
    },
    reloadAreaTree:{
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      noData:true,
      curPagePath:''
    }
  },
  mounted() {
    let userLoginId = window.sessionStorage.getItem('loginid')
    this.curPagePath = userLoginId+'-'+this.$route.path
    let areaTreeData = this.adjustAreaTreeData()
    this.initAreaTree('areaTableTree',areaTreeData)
  },
  methods: {
    // 初始化区域树
    initAreaTree(treeId,areaTreeData){
      let self = this
      $.fn.zTree.destroy(treeId);
      if(!areaTreeData.length && !self.areaTreeData.length){
        this.noData = true
        return
      }
      let settings = {
        data: {
          simpleData: {
            enable: true
          }
        },
        check: {
          enable: true,
          chkStyle: "checkbox",
          chkboxType: {
            "Y" : "", 
            "N" : ""
          }
        },
        view: {
          showIcon: false,
          showLine: false,
        },
        callback: {
          onClick: self.OnClickNode,
          onCheck: self.OnCheckNode
        }
      };
      let treeObj = null
      if(areaTreeData && areaTreeData.length>0){
        this.noData = false
        treeObj = $.fn.zTree.init($("#"+treeId), settings, areaTreeData);
      }else{
        treeObj = $.fn.zTree.init($("#"+treeId), settings, self.areaTreeData);
      }
    },
    OnClickNode(event, treeId, treeNode) {
      let isChecked = treeNode.checked;
      let treeObj = $.fn.zTree.getZTreeObj("areaTableTree");
      treeObj.checkNode(treeNode, !isChecked, false);
      this.OnCheckNode(event, treeId, treeNode)
    },
    OnCheckNode(event, treeId, treeNode) {
      let parkIds = []
      var treeObj = $.fn.zTree.getZTreeObj("areaTableTree");
      let nodes = treeObj.getCheckedNodes(true);
      for (let i = 0; i < nodes.length; i++) {
        if(parkIds.indexOf(nodes[i].parkId)==-1){
          parkIds.push(nodes[i].parkId)
        }
      }
      let allNodes = treeObj.getNodes()
      for(let i = 0; i<allNodes.length; i++){
        if(i!=0 && parkIds.indexOf(allNodes[i].parkId)==-1){
          let curNode = treeObj.getNodeByParam("parkId", allNodes[i].parkId, null);
          treeObj.expandNode(curNode, false, true, false);
        }
      }
    },
    splitWord(str) {
      if (str.indexOf("(") != -1) {
        var strs = str.split("(", 2);
        return strs[0];
      } else {
        return str;
      }
    },
    setSelectedAreaNode(areaIds, areaNames) {
      let searchObj = {};
      searchObj[this.paramNameForAreaTree] = areaIds;
      searchObj['areaNames'] = areaNames
      this.$emit('setSelectedAreaNode', searchObj);
    },
    adjustAreaTreeData(){
      let curPageCache = window.localStorage.getItem(this.curPagePath)
      let _curPageCache = curPageCache ? JSON.parse(curPageCache) : {}
      let areaTreeData = _curPageCache.areaTreeData ? JSON.parse(_curPageCache.areaTreeData) : []
      let ids = _curPageCache.areaIds ? _curPageCache.areaIds : ''
      for(let i = 0; i<areaTreeData.length; i++){
        let itemObj = areaTreeData[i] //停车场
        let rootAreaObj = itemObj.children[0] //根区域
        if(ids.indexOf(rootAreaObj.id)>-1){
          rootAreaObj.checked = true
          rootAreaObj.open = true
          itemObj.open = true
        }else{
          if(i!=0){
            rootAreaObj.open = false
            itemObj.open = false
          }else{
            rootAreaObj.open = true
            itemObj.open = true
          }
        }
        let dataList = rootAreaObj.children && rootAreaObj.children.length? rootAreaObj.children : []
        for(let j = 0; j<dataList.length; j++){
          let tmpObj = dataList[j]
          if(ids.indexOf(tmpObj.id)>-1){
            tmpObj.checked = true
            itemObj.open = true
          }else{
            if(i!=0){
              tmpObj.open = false
              itemObj.open = false
              rootAreaObj.open = false
            }else{
              tmpObj.open = true
              itemObj.open = true
              rootAreaObj.open = true
            }
          }
        }
      }
      return areaTreeData
    },
  },
  watch:{
    'reloadAreaTree':function(){
      let areaTreeData = this.adjustAreaTreeData()
      this.initAreaTree('areaTableTree',areaTreeData)
    },
  },
}